package com.infore.exchange.main.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.infore.common.fw.DateUnit;
import com.infore.common.model.dto.ChartsVo;
import com.infore.common.model.dto.KeyValueVo;
import com.infore.common.model.entity.exchange.TExTransformLog;
import com.infore.common.model.entity.vo.exchange.TExTransformLogVo;
import com.infore.common.request.query.exchange.TransformLogRequest;
import com.infore.common.utils.DateUtils;
import com.infore.common.utils.NumberUtil;
import com.infore.exchange.main.dao.TExTransformLogMapper;
import com.infore.exchange.main.service.TExTransformLogService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 数据交换日志 服务实现类
 * </p>
 *
 * @author lijh
 * @since 2023-02-21
 */
@Service
public class TExTransformLogServiceImpl extends ServiceImpl<TExTransformLogMapper, TExTransformLog> implements TExTransformLogService {


    @Autowired
    private TExTransformLogMapper transformLogMapper;


    /**
    * @Deacription 数据交换读写行数
    * @MethodName queryDataTransformReadAndWriterRows
    * @param
    * @return {@link Map< String, Object>}
    * @Author lijianhua
    * @Date 2023/2/23 14:15
    **/
    @Override
    public Map<String, Object> queryDataTransformReadAndWriterRows() {
        Map<String, Object> returnMap = new HashMap<>();
        KeyValueVo sendSum = new KeyValueVo();
        KeyValueVo receiveSum = new KeyValueVo();
        sendSum.setKey("send");
        receiveSum.setKey("receive");
        Map<String, Object> map = transformLogMapper.queryDataTransformReadAndWriterRows();
        if(map!=null){
            String sendSumStr = map.get("sendSum")==null? "0":  map.get("sendSum").toString();
            String receiveSumStr = map.get("receiveSum")==null? "0":  map.get("receiveSum").toString();
            Map<String, Object> sendValUnit = NumberUtil.getValUnit(Long.parseLong(sendSumStr));
            sendSum.setValue(sendValUnit.get("val"));
            sendSum.setUnit(sendValUnit.get("unit")==null? "条":sendValUnit.get("unit").toString()+"条");

            Map<String, Object>  receiveValUnit = NumberUtil.getValUnit(Long.parseLong(receiveSumStr));
            receiveSum.setValue(receiveValUnit.get("val"));
            receiveSum.setUnit(receiveValUnit.get("unit")==null? "条":receiveValUnit.get("unit").toString()+"条");
        }else{
            sendSum.setValue(0);
            sendSum.setUnit("条");
            receiveSum.setValue(0);
            receiveSum.setUnit("条");
        }
        returnMap.put("sendSum",sendSum);
        returnMap.put("receiveSum",receiveSum);
        return  returnMap;
    }

    /**
    * @Deacription 任务交换日志
    * @MethodName queryTransformLogByLastday
    * @return {@link List< TExTransformLogVo>}
    * @Author lijianhua
    * @Date 2023/2/24 13:45
    **/
    @Override
    public List<TExTransformLogVo> queryTransformLogByLastday() {
        return transformLogMapper.queryTransformLogByLastday();
    }


    /**
    * @Deacription 交换数据统计
    * @MethodName queryTransformLogReadWriterStatistics
    * @param format
    * @param days
    * @return {@link List< Map< String, Object>>}
    * @Author lijianhua
    * @Date 2023/2/24 13:44
    **/
    @Override
    public List<Map<String,Object>> queryTransformLogReadWriterStatistics(String format, int days) {
        return transformLogMapper.queryTransformLogReadWriterStatistics(format,days-1);
    }

    /**
     * 日志查询
     * @param request
     * @return
     */
    @Override
    public List<Map<String,Object>> queryTransformLog(TransformLogRequest request) {
        return transformLogMapper.queryTransformLog(request);
    }


    /**
    * @Deacription 数据交换按天统计折线图-最近30天
    * @MethodName queryTransformLogReadWriterStatisticsOfLastMonth
    * @return {@link ChartsVo}
    * @Author lijianhua
    * @Date 2023/2/24 16:22
    **/
    @Override
    public ChartsVo queryTransformLogReadWriterStatisticsOfLastMonth() {
        String startDateStr = DateUtil.format(DateUtil.offsetDay(new Date(), -29),DateUtils.FORMAT_05) ;
        String endDateStr=DateUtil.format(new Date(),DateUtils.FORMAT_05);
        List<String> xList = DateUtils.getDateStrBetween(startDateStr, endDateStr, DateUnit.DAY, DateUtils.FORMAT_05);
        List<Map<String, Object>> list = transformLogMapper.queryTransformLogReadWriterStatistics("%Y-%m-%d", 30-1);

        return getChartsVo(xList, list);
    }

    /**
    * @Deacription 数据交换按月统计折线图-最近1年
    * @MethodName queryTransformLogReadWriterStatisticsOfLastYear
    * @return {@link ChartsVo}
    * @Author lijianhua
    * @Date 2023/2/24 16:23
    **/
    @Override
    public ChartsVo queryTransformLogReadWriterStatisticsOfLastYear() {
        String startDateStr = DateUtil.format(DateUtil.offsetMonth(new Date(), -11),DateUtils.FORMAT_03) ;
        String endDateStr=DateUtil.format(new Date(),DateUtils.FORMAT_03);
        List<String> xList = DateUtils.getDateStrBetween(startDateStr, endDateStr, DateUnit.MONTH, DateUtils.FORMAT_03);
        List<Map<String, Object>> list = transformLogMapper.queryTransformLogReadWriterStatistics("%Y-%m", 365-1);
        return getChartsVo(xList, list);
    }

    private ChartsVo getChartsVo(List<String> xList, List<Map<String, Object>> list) {
        ChartsVo vo = new ChartsVo();
        List<Map<String, Object>> series = new ArrayList<>();
        Map<String, Object> readSerie = new HashMap<>();
        Map<String, Object> writerSerie = new HashMap<>();

        List<String> legend = new ArrayList<>();
        legend.add("数据发送");
        legend.add("数据接收");

        List<Object> readYAxis = new ArrayList<>();
        List<Object> writerYAxis = new ArrayList<>();

        for (String xaixs : xList) {
            Object readDataValue = 0;
            Object writerDataValue = 0;
            for (Map<String, Object> map : list) {
                String dateStr = map.get("dateStr").toString();
                if (xaixs.equals(dateStr)) {
                    readDataValue = map.get("readRows");
                    writerDataValue = map.get("writerRows");
                    break;
                }
            }
            readYAxis.add(readDataValue);
            writerYAxis.add(writerDataValue);
        }
        readSerie.put("name", "数据发送");
        readSerie.put("unit", "行");
        readSerie.put("data", readYAxis);
        readSerie.put("type", "line");

        writerSerie.put("name", "数据接收");
        writerSerie.put("unit", "行");
        writerSerie.put("data", writerYAxis);
        writerSerie.put("type", "line");

        series.add(readSerie);
        series.add(writerSerie);

        vo.setLegend(legend);
        vo.setXAxis(xList);
        vo.setSeries(series);
        return vo;
    }


}
